package org.wyn.mongodb.dao.impl;

import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.common.SearchBDProvider;

import com.google.code.morphia.dao.BasicDAO;
/**
 * 用户详细资料mongodb dao层(实现)
 * @author wyn
 *
 */
public class UserDetailDao extends BasicDAO<UserDetail, String> {
	public UserDetailDao(){
		super(SearchBDProvider.getInstance());
	}
	/**
	 * 改变用户的头像
	 * @param userName 用户账号
	 * @param imageId 照片id
	 * @return
	 */
	public boolean changePortrait(String userName,ObjectId imageId){
		UserDetail ud = findOne(createQuery().filter("userName", userName));
		if(null == ud)
			return false;
		ud.setPortrait(imageId);
		save(ud);
		ensureIndexes();
		return true;
	}
	/**
	 * 找到用户的头像,用户不存在返回new ObjectId()
	 * @param userName
	 * @return
	 */
	public ObjectId findPortrait(String userName){
		UserDetail ud = findOne("userName", userName);
		if(null == ud){
			return new ObjectId();
		}else{
			return ud.getPortrait();
		}
	}
	public static void main(String[] args) {
		UserDetailDao udd = new UserDetailDao();
		udd.save(UserDetail.getTestInstance("wwmm.cs@gmail.com", "male"));
		udd.ensureIndexes();
	}
}
